 /***************************************************************************
 * Copyright (C) 2012 Dj Skual
 * for SaveGame Manager GX 2012
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 ***************************************************************************/
#include "Update.h"
#include "UpdateDolTask.h"
#include "UpdateIconTask.h"
#include "UpdateMetaTask.h"
#include "UpdateGameTDBTask.h"
#include "../../Tools/ErrorLog.h"
#include "../../Controls/Application.h"
#include "../../Prompts/PromptWindows.h"
#include "../../Prompts/ProgressWindow.h"
#include "../../Language/UpdateLanguagesTask.h"

/****************************************************************************
 * UpdateApp from a given url. The dol is downloaded and overwrites the old one.
 ***************************************************************************/
bool UpdateApp(const char *url)
{
	if(!url)
	{
		ShowError(tr("URL is empty."));
		return false;
	}
	
	UpdateDolTask * task = new UpdateDolTask(url);
	
	while(task->IsRunning())
		Application::Instance()->updateEvents();
	
	bool succes = false;
	if(task->Succes())
		succes = true;
	
	delete task;
	
	if(succes)
		UpdateFiles();
	
	StopProgress();
	
	return succes;
}

/****************************************************************************
 * Update files selected in settings
 ***************************************************************************/
 bool UpdateFiles()
{
	ErrorLog::DestroyInstance();
	
	//! Update meta.xml
	UpdateMetaTask * metaTask = new UpdateMetaTask();
	
	while(metaTask->IsRunning())
		Application::Instance()->updateEvents();
	
	if(!metaTask->Succes())
		ErrorLog::Instance()->AddEntry("meta.xml", tr("Update failed"));
	
	delete metaTask;
	
	//! Update icon.png
	if(Settings.UpdateIcon > 0)
	{
		UpdateIconTask * iconTask = new UpdateIconTask();
		
		while(iconTask->IsRunning())
			Application::Instance()->updateEvents();
		
		if(!iconTask->Succes())
			ErrorLog::Instance()->AddEntry("icon.png", tr("Update failed"));
		
		delete iconTask;
	}
	
	//! Update languages files
	if(Settings.UpdateLanguage > 0)
	{
		UpdateLanguagesTask * langTask = NULL;
		
		if (Settings.UpdateLanguage == LANG_UPDATE_ALL)
			langTask = new UpdateLanguagesTask(Settings.LanguagePath, UpdateLanguagesTask::ALL);
		else if (Settings.UpdateLanguage == LANG_UPDATE_INSTALLED)
			langTask = new UpdateLanguagesTask(Settings.LanguagePath, UpdateLanguagesTask::INSTALLED);
		
		if(langTask)
		{
			while(langTask->IsRunning())
				Application::Instance()->updateEvents();
			
			if(!langTask->Succes())
			{
				if(Settings.UpdateLanguage == LANG_UPDATE_ALL)
					ErrorLog::Instance()->AddEntry(tr("All Languages Files"), tr("Update failed"));
				else if(Settings.UpdateLanguage == LANG_UPDATE_INSTALLED)
					ErrorLog::Instance()->AddEntry(tr("Installed Languages Files"), tr("Update failed"));
			}
			
			delete langTask;
		}
	}
	
	//! Update GameTDB
	if(Settings.UpdateGameTDB)
	{
		UpdateGameTDBTask * GameTDBTask = new UpdateGameTDBTask();
		
		while(GameTDBTask->IsRunning())
			Application::Instance()->updateEvents();
		
		if(!GameTDBTask->Succes())
			ErrorLog::Instance()->AddEntry("GameTDB", tr("Update failed"));
		
		delete GameTDBTask;
	}
	
	StopProgress();
	
	if(ErrorLog::Instance()->GetCount())
	{
		if(WindowPrompt(tr("Error"), tr("Show error log?"), tr("OK"), tr("Cancel")))
			ErrorLog::Instance()->Show();
	}
	
	return (!ErrorLog::Instance()->GetCount());
}
